package com.guanglan.serviceImpl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.guanglan.dao.SysUserMapper;
import com.guanglan.domain.SysUser;
import com.guanglan.service.SysUserRoleService;
import com.guanglan.service.SysUserService;
import com.guanglan.utils.CreatePassWordUtil;

@Service
public class SysUserServiceImpl implements SysUserService {
	
	Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private SysUserMapper userMapper;
	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Transactional
	public int deleteByPrimaryKey(Long id) {
		int delUserResult = userMapper.deleteByPrimaryKey(id);
		log.info("删除用户信息{}",delUserResult > 0 ? "成功":"失败");
		int delUserRoleResult = sysUserRoleService.deleteByUserId(id);
		log.info("删除用户关联角色信息{}",delUserRoleResult > 0 ? "成功":"失败");
		return delUserResult;
	}

	public int insert(SysUser record) {
		// TODO Auto-generated method stub
		return userMapper.insert(record);
	}

	public int insertSelective(SysUser sysUser) {
		//密码加盐进行1024次MD5加密
		String salt = String.valueOf((int)(Math.random()*1000000+1));
		sysUser.setSalt(salt);
		sysUser.setPassWord(CreatePassWordUtil.getPwd(sysUser.getPassWord(), salt));
		return userMapper.insertSelective(sysUser);
	}

	public SysUser selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(SysUser record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(SysUser record) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKey(record);
	}

	public SysUser selectByUserName(String userName) {
		// TODO Auto-generated method stub
		return userMapper.selectByUserName(userName);
	}

	@Override
	public List<SysUser> selectSysUsers(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return userMapper.selectSysUsers(map);
	}

	@Override
	public Integer selectSysUsersCount(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return userMapper.selectSysUsersCount(map);
	}
	
	

}
